/*
 * Copyright (c) 2020-2021, Bluetrum Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2020/12/10     greedyhao    The first version
 */

/**
 * Notice!
 * All functions or data that are called during an interrupt need to be in RAM.
 * You can do it the way exception_isr() does.
 */
#include <rtthread.h>
#include "board.h"
#include <driver/led.h>
#include <driver/key.h>
#include <driver/fan.h>
#include <driver/oled.h>
#include <protocal/uart_protocal.h>

int show_temp = 0;
int show_humi = 0;
int show_light = 69;

int show_fan_flag = 0;
int show_fan_val = 0;
int show_ptc_flag = 0;
int show_ptc_val = 0;
int show_hdr_flag = 0;
int show_hdr_val = 0;

rt_thread_t send_thread;

//开启传感器发送线程
static void send_thread_entry(void *parameter)
{
    uint8_t temp_val = 22;
    uint8_t humi_val = 18;
    uint8_t light_val = 102;
    while(1)
    {
        //发送温度
        // if(temp_val>40)
        // {
        //     temp_val = 0;
        // }
        // else
        // {
        //     temp_val++;
        // }
        rt_kprintf("send temp val is %d\n", show_temp);
        ab_send_msg(AB_TEMP, 0, show_temp);
        rt_thread_mdelay(1000 * 10); //10s 上传

        //发送湿度
        // if(humi_val>40)
        // {
        //     humi_val = 0;
        // }
        // else
        // {
        //     humi_val++;
        // }
        rt_kprintf("send temp val is %d\n", show_humi);
        ab_send_msg(AB_HUMI, 0, show_humi);
        rt_thread_mdelay(1000 * 10); //10s 上传

        //发送光线强度
        // if(light_val>500)
        // {
        //     light_val = 0;
        // }
        // else
        // {
        //     light_val++;
        // }
        rt_kprintf("send temp val is %d\n", 69);
        ab_send_msg(AB_LIGHT, 0, show_light);
        rt_thread_mdelay(1000 * 10); //10s 上传
    }
}

void sensor_send_task(void)
{
    send_thread = rt_thread_create("sendto", send_thread_entry, RT_NULL, 1024, 18, 10);
    if (send_thread != RT_NULL)
    {
        rt_thread_startup(send_thread);
    }
    else
    {
        rt_kprintf("creat task err!\n");
    }
}

int main(void)
{
    uint32_t val;
    rt_uint8_t temp,humi;
    int cnt = 0;
    int show_flag = 0;
    rt_kprintf("Hello, world\n");
    ssd1306_bitmap();
    dht11_init_pin();
    ab32_uart1_init();
    sensor_send_task();
    blux_v20_Init();
    fan_init();
    hdr_init();
    ptc_init();
    fan_off();
    hdr_off();
    ptc_off();

    //RGBLeds_Init();
    while (1)
    {
        //RunningWaterLamp();
        //获取LED值
        if(get_ctrl_val(ESP_LED, &val))
        {
            rt_kprintf("led val is %d\n", val);
        }

        //获取FAN值
        if(get_ctrl_val(ESP_FAN, &val))
        {
            show_fan_flag = 1;
            show_fan_val = val;
            if(val == 1)
            {
                fan_on();
            }
            else {
                fan_off();
            }
            rt_kprintf("fan val is %d\n", val);
        }

        //获取PTC值
        if(get_ctrl_val(ESP_PTC, &val))
        {
            show_ptc_flag = 1;
            show_ptc_val = val;
            if(val == 1)
                {
                    ptc_on();
                }
                else {
                    ptc_off();
                }
            rt_kprintf("ptc val is %d\n", val);
        }

        //获取加湿器值
        if(get_ctrl_val(ESP_HUMIDIFIER, &val))
        {
            show_hdr_flag = 1;
            show_hdr_val = val;
            if(val == 1)
                 {
                     hdr_on();
                 }
                 else {
                     hdr_off();
                 }
            rt_kprintf("humidifier val is %d\n", val);
        }
        rt_thread_mdelay(500);

        get_temp_humi(&temp, &humi);
        show_temp = temp;
        show_humi = humi;
       // show_light = range()%100;

        if(show_fan_flag || show_ptc_flag || show_hdr_flag)
        {
            //说明有数据,立即刷新
            show_net_set(show_fan_flag, show_ptc_flag, show_hdr_flag, val);
            cnt = 0;
            show_fan_flag = 0;
            show_ptc_flag = 0;
            show_hdr_flag = 0;
        }

        cnt++;
        if(cnt >= 10)
        {
            if(show_flag == 0)
            {
                ssd1306_set_main_1(show_temp, show_humi, show_light);
                show_flag = 1;
//                fan_on();
//                hdr_off();
//                ptc_off();
            }
            else if(show_flag == 1)
            {
                ssd1306_set_main_2(show_fan_val, show_ptc_val, show_hdr_val);
                show_flag = 0;
//                fan_off();
//                hdr_on();
//                ptc_on();
            }
            else
            {

            }
            cnt = 0;
        }
    }
}
